package com.itmk.web.notice_category.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.notice_category.entity.CatePage;
import com.itmk.web.notice_category.entity.NoticeCategory;
import com.itmk.web.notice_category.service.NoticeCategoryService;
import com.itmk.web.sys_major.entity.Major;
import com.itmk.web.sys_major.entity.SelectMajor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author java实战基地
 * @since 2023-10-13
 */
@RestController
@RequestMapping("/api/noticeCategory")
public class NoticeCategoryController {
    @Autowired
    private NoticeCategoryService noticeCategoryService;

    //新增
    @PostMapping
    public ResultVo add(@RequestBody NoticeCategory category){
        if(noticeCategoryService.save(category)){
            return ResultUtils.success("新增成功!");
        }
        return ResultUtils.error("新增失败!");
    }

     //编辑
    @PutMapping
    public ResultVo edit(@RequestBody NoticeCategory category){
        if(noticeCategoryService.updateById(category)){
            return ResultUtils.success("编辑成功!");
        }
        return ResultUtils.error("编辑失败!");
    }

     //删除
    @DeleteMapping("/{cagegoryId}")
    public ResultVo delete(@PathVariable("cagegoryId") Integer cagegoryId){
        if(noticeCategoryService.removeById(cagegoryId)){
            return ResultUtils.success("删除成功!");
        }
        return ResultUtils.error("删除失败!");
    }

    //列表
    @GetMapping("/list")
    public ResultVo list(CatePage parm){
        //构造分页对象
        IPage<NoticeCategory> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());
        //构造查询条件
        QueryWrapper<NoticeCategory> query = new QueryWrapper<>();
        query.lambda().like(StringUtils.isNotEmpty(parm.getCategoryName()),NoticeCategory::getCategoryName,parm.getCategoryName())
                .orderByDesc(NoticeCategory::getOrderNum);
        IPage<NoticeCategory> list = noticeCategoryService.page(page, query);
        return ResultUtils.success("查询成功",list);
    }

    //分类下拉数据
    @GetMapping("/select")
    public ResultVo select(){
        List<NoticeCategory> list = noticeCategoryService.list();
        List<SelectMajor> selectMajors = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .stream()
                .forEach(item ->{
                    SelectMajor major = new SelectMajor();
                    major.setLabel(item.getCategoryName());
                    major.setValue(item.getCagegoryId());
                    selectMajors.add(major);
                });
        return ResultUtils.success("查询成功",selectMajors);
    }
}

